home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / ld / mri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-20  |  8.0 KB  |  377 lines

  1. /* Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  2.    
  3. This file is part of GLD, the Gnu Linker.
  4.  
  5. GLD is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GLD is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GLD; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. /* This bit does the tree decoration when MRI style link scripts are parsed */
  21.  
  22. /*
  23.   contributed by Steve Chamberlain
  24.            sac@cygnus.com
  25.  
  26. */
  27.  
  28. #include "bfd.h"
  29. #include "sysdep.h" 
  30. #include "ld.h"
  31. #include "ldexp.h"
  32. #include "ldlang.h"
  33. #include "ldmisc.h"
  34. #include "mri.h"
  35. #include "ldgram.h"
  36.  
  37.  
  38. struct section_name_struct {
  39.   struct section_name_struct *next;
  40.   CONST char *name;
  41.   CONST char *alias;
  42.   etree_type *vma;
  43.   etree_type *align;
  44.   etree_type *subalign;
  45.   int ok_to_load;
  46. } ;
  47.  
  48. unsigned int symbol_truncate = 10000;
  49. struct section_name_struct *order;
  50. struct section_name_struct *only_load;
  51. struct section_name_struct *address;
  52. struct section_name_struct *alias;
  53.  
  54. struct section_name_struct *alignment;
  55. struct section_name_struct *subalignment;
  56.  
  57. extern char *strdup();
  58.  
  59. static struct section_name_struct **lookup
  60.   PARAMS ((const char *name, struct section_name_struct **list));
  61. static void mri_add_to_list PARAMS ((struct section_name_struct **list,
  62.                      const char *name, etree_type *vma,
  63.                      const char *zalias, etree_type *align,
  64.                      etree_type *subalign));
  65. static void mri_draw_tree PARAMS ((void));
  66.  
  67. static struct section_name_struct **
  68. lookup (name, list)
  69.      CONST char *name;
  70.      struct section_name_struct **list;
  71. {
  72.  
  73.   struct section_name_struct **ptr = list;
  74.   while (*ptr) 
  75.   {
  76.     if (strcmp(name, (*ptr)->name) == 0) {
  77.       /* If this is a match, delete it, we only keep the last instance
  78.      of any name */
  79.       *ptr = (*ptr)->next;
  80.     }
  81.     else {
  82.       ptr = &((*ptr)->next);
  83.     }
  84.   }
  85.  
  86.   *ptr = (struct section_name_struct *)xmalloc(sizeof(struct section_name_struct));
  87.   return ptr;
  88. }
  89.  
  90. static void
  91. mri_add_to_list (list, name, vma, zalias, align, subalign)
  92.      struct section_name_struct **list;
  93.      CONST char *name;
  94.      etree_type *vma;
  95.      CONST char *zalias;
  96.      etree_type *align;
  97.      etree_type *subalign;
  98. {
  99.   struct section_name_struct **ptr = lookup(name,list);
  100.   (*ptr)->name = name;
  101.   (*ptr)->vma = vma;
  102.   (*ptr)->next = (struct section_name_struct *)NULL;
  103.   (*ptr)->ok_to_load = 0;
  104.   (*ptr)->alias = zalias;
  105.   (*ptr)->align = align;
  106.   (*ptr)->subalign = subalign;
  107. }
  108.  
  109.  
  110. void
  111. mri_output_section (name, vma)
  112.      CONST char *name;
  113.      etree_type *vma;
  114. {
  115.   mri_add_to_list(&address, name, vma, 0,0,0);
  116. }
  117.  
  118. /* if any ABSOLUTE <name> are in the script, only load those files
  119. marked thus */
  120.  
  121. void
  122. mri_only_load (name)
  123.      CONST char *name;
  124. {
  125.   mri_add_to_list(&only_load, name, 0, 0,0,0);
  126. }
  127.  
  128.  
  129. void
  130. mri_base (exp)
  131.      etree_type *exp;
  132. {
  133.   base = exp;
  134. }
  135.  
  136. static int done_tree = 0;
  137.  
  138. static void
  139. mri_draw_tree ()
  140. {
  141.   if (done_tree) return;
  142.  
  143.   /* Create the regions */
  144.  {
  145.    lang_memory_region_type *r;
  146.    r = lang_memory_region_lookup("long");
  147.    r->current = r->origin = exp_get_vma(base, (bfd_vma)0, "origin",
  148.                     lang_first_phase_enum);
  149.    r->length = (bfd_size_type) exp_get_vma(0, (bfd_vma) ~((bfd_size_type)0),
  150.                        "length", lang_first_phase_enum);
  151.  }
  152.  
  153.   
  154.   /* Now build the statements for the ldlang machine */
  155.  
  156.  
  157.   /* Attatch the addresses of any which have addresses, and add the
  158.      ones not mentioned */
  159.   if (address != (struct section_name_struct *)NULL) {
  160.     struct section_name_struct *alist;
  161.     struct section_name_struct *olist;
  162.     if (order == (struct section_name_struct *)NULL) {
  163.       order = address;
  164.     }
  165.  
  166.     for (alist = address;
  167.      alist != (struct section_name_struct*)NULL;
  168.      alist = alist->next) 
  169.     {
  170.       int done = 0;
  171.       for (olist = order;
  172.        done == 0 &&
  173.        olist != (struct section_name_struct *)NULL;
  174.        olist = olist->next) 
  175.       {
  176.     if (strcmp(alist->name, olist->name) == 0) 
  177.     {
  178.       olist->vma = alist->vma;
  179.       done = 1;
  180.     }
  181.       }
  182.       if (!done) {
  183.     /* add this onto end of order list */
  184.     mri_add_to_list(&order, alist->name, alist->vma, 0,0,0);
  185.       }
  186.  
  187.     }
  188.  
  189.   }
  190.  
  191.   /* If we're only supposed to load a subset of them in, then prune
  192.      the list.  */
  193.  
  194.   if (only_load != (struct section_name_struct *)NULL) 
  195.   {
  196.     struct section_name_struct *ptr1;
  197.     struct section_name_struct *ptr2;
  198.     if (order == (struct section_name_struct*)NULL)
  199.      order = only_load;
  200.     
  201.     /* See if this name is in the list, if it is then we can load it
  202.      */
  203.     for (ptr1 = only_load; ptr1; ptr1 = ptr1->next) 
  204.     {
  205.       for (ptr2= order; ptr2; ptr2=ptr2->next) 
  206.       {
  207.     if (strcmp(ptr2->name, ptr1->name)==0) {
  208.       ptr2->ok_to_load = 1;
  209.     }
  210.       }
  211.     }
  212.   }
  213.   else 
  214.   {
  215.     /* No only load list, so everything is ok to load */
  216.     struct section_name_struct *ptr;
  217.     for (ptr = order; ptr; ptr=ptr->next) {
  218.       ptr->ok_to_load = 1;
  219.     }
  220.   }
  221.  
  222.  
  223.  
  224.   /* Create the order of sections to load */
  225.   if (order != (struct section_name_struct *)NULL) 
  226.   {
  227.     /* Been told to output the sections in a certain order */
  228.     struct section_name_struct *p = order;
  229.     while (p) 
  230.     {
  231.       struct section_name_struct *aptr;
  232.       etree_type *align = 0;
  233.       etree_type *subalign = 0;
  234.       /* See if an alignment has been specified */
  235.  
  236.       for (aptr = alignment; aptr; aptr= aptr->next)
  237.       {
  238.     if (strcmp(aptr->name, p->name)==0) {
  239.       align =  aptr->align;
  240.     }
  241.       }
  242.  
  243.       for (aptr = subalignment; aptr; aptr= aptr->next)
  244.       {
  245.     if (strcmp(aptr->name, p->name)==0) {
  246.       subalign =  aptr->subalign;
  247.     }
  248.       }
  249.  
  250.       if (base == 0) {
  251.     base = p->vma ? p->vma :exp_nameop(NAME, ".");
  252.       }
  253.       lang_enter_output_section_statement (p->name, base,
  254.                        p->ok_to_load ? 0 : SEC_NEVER_LOAD,
  255.                        1, align, subalign,
  256.                        (etree_type *) NULL);
  257.       base = 0;
  258.       lang_add_wild(p->name, (char *)NULL);
  259.       /* If there is an alias for this section, add it too */
  260.       for (aptr = alias; aptr; aptr = aptr->next) {
  261.  
  262.     if (strcmp(aptr->alias, p->name)== 0) {
  263.       lang_add_wild(aptr->name, (char *)NULL);
  264.     }
  265.       }
  266.     
  267.       lang_leave_output_section_statement(0, "long");
  268.       p = p->next;
  269.     }
  270.   }
  271.  
  272.  
  273.   done_tree = 1;
  274.  
  275. }
  276. void
  277. mri_load (name)
  278.      CONST char *name;
  279. {
  280.   mri_draw_tree();
  281.  
  282.   base = 0;
  283.   lang_add_input_file(name,
  284.               lang_input_file_is_file_enum, (char *)NULL);
  285.   /*  lang_leave_output_section_statement(0,"*default*");*/
  286. }
  287.  
  288.  
  289. void
  290. mri_order (name)
  291.      CONST char *name;
  292. {
  293.   mri_add_to_list(&order, name, 0, 0,0,0);
  294. }
  295.  
  296. void 
  297. mri_alias (want, is, isn)
  298.      CONST char *want;
  299.      CONST char *is;
  300.      int isn;
  301. {
  302.   if (!is) {
  303.     /* Some sections are digits - */
  304.     char buf[20];
  305.     sprintf(buf, "%d", isn);
  306.     is =strdup(buf);
  307.     if (is == NULL)
  308.       abort ();
  309.   }
  310.   mri_add_to_list(&alias, is, 0, want,0,0);
  311.  
  312. }
  313.  
  314.  
  315. void 
  316. mri_name (name)
  317.      CONST char *name;
  318. {
  319.   lang_add_output(name, 1);
  320.  
  321. }
  322.  
  323.  
  324. void
  325. mri_format (name)
  326.      CONST char *name;
  327. {
  328.   if (strcmp(name, "S") == 0)
  329.   {
  330.     lang_add_output_format("srec", (char *) NULL, (char *) NULL, 1);
  331.   }
  332.   else if (strcmp(name, "IEEE") == 0)
  333.   {
  334.     lang_add_output_format("ieee", (char *) NULL, (char *) NULL, 1);
  335.   }
  336.   else if (strcmp(name, "COFF") == 0)
  337.   {
  338.     lang_add_output_format("coff-m68k", (char *) NULL, (char *) NULL, 1);
  339.   }
  340.   else {
  341.     einfo("%P%F: unknown format type %s\n", name);
  342.   }
  343. }
  344.  
  345.  
  346. void
  347. mri_public (name, exp)
  348.      CONST char *name;
  349.      etree_type *exp;
  350. {
  351.   lang_add_assignment(exp_assop('=', name, exp));
  352. }
  353.  
  354. void 
  355. mri_align (name, exp)
  356.      CONST char *name;
  357.      etree_type *exp;
  358. {
  359.   mri_add_to_list(&alignment, name,0,0,exp,0);
  360. }
  361.  
  362. void 
  363. mri_alignmod (name, exp)
  364.      CONST char *name;
  365.      etree_type *exp;
  366. {
  367.   mri_add_to_list(&subalignment, name,0,0,0,exp);
  368. }
  369.  
  370.  
  371. void 
  372. mri_truncate (exp)
  373.      unsigned int exp;
  374. {
  375.   symbol_truncate = exp;
  376. }
  377.